package com.itheima.d1_arrays;

import java.util.Arrays;
import java.util.function.IntToDoubleFunction;

/**
 * 目标：掌握Arrays类的常用方法。
 */
public class ArraysTest1 {
    public static void main(String[] args) {
        int[] arr = {10, 20, 30, 40, 50, 60};
        //           0    1   2  3   4   5
        // 1、public static String toString(类型[] arr): 返回数组的内容
        System.out.println("arr = " + arr);
        System.out.println(Arrays.toString(arr));//[10, 20, 30, 40, 50, 60]


        // 2、public static 类型[] copyOfRange(类型[] arr, 起始索引, 结束索引) ：拷贝数组（指定范围，包前不包后）
        int[] newArr1 = Arrays.copyOfRange(arr, 1, 4);
        System.out.println("Arrays.toString(newArr1) = " + Arrays.toString(newArr1));


        // 3、public static 类型[] arr copyOf(类型[] arr, int newLength)：拷贝数组，可以指定新数组的长度。
        int[] newArr2 = Arrays.copyOf(arr, 10);
        System.out.println("Arrays.toString(newArr2) = " + Arrays.toString(newArr2));


        // 4、public static void setAll(double[] array, IntToDoubleFunction generator)：把数组中的原数据改为新数据又存进去。
        double[] prices = {99.8, 128, 100};
        // 把所有的价格都打八折，然后又存进去。
        Arrays.setAll(prices, new IntToDoubleFunction() {
            @Override
            public double applyAsDouble(int index) {//方法参数中的数据不是元素而是每个元素对应的索引
                //打8折
                return prices[index]*0.8;//返回值会把index索引的数据替换
            }
        });

        Arrays.setAll(prices,(int index)->{
            return prices[index] * 0.8;
        });


        System.out.println("Arrays.toString(prices) = " + Arrays.toString(prices));



        // 5、public static void sort(类型[] arr)：对数组进行排序(默认是升序排序)
        int[] newArr3 = {10, 30, 20, 50, 40};
        Arrays.sort(newArr3);//在数组内部进行排序，没有返回新的数组
        System.out.println("Arrays.toString(newArr3) = " + Arrays.toString(newArr3));//[10, 20, 30, 40, 50]

        String[] newArr4 = {"Jack","Rose","jack","rose","Carry","Marry","Andrew","andy","Andy"};
        Arrays.sort(newArr4);
        System.out.println("Arrays.toString(newArr4) = " + Arrays.toString(newArr4));
        //[Andrew, Carry, Jack, Marry, Rose, andy, jack, rose]



    }
}
